<?php
namespace app\admin\controller;
use app\common\controller\Admin ; 

class Myself extends Admin
{
    


    //控制器初始化
    public function _initialize()
    {
        parent::_initialize();
        $this->model =  model('User');
    }


    public function index()
    {
        if(IS_AJAX){
            $data   = $this->request->post();

            $allowField = [ 'username' , 'status' ,'sex' ,'birthday' ,  'mobile' ,   'workphone'  ,  'qq'  ,   'email'  , 'idcard' , 'address' ];
           
            //判断是 添加 还是 修改

            $scene = 'user.edit';

// dump($data);
            if ($data) {

                $result = $this->model
                    ->isUpdate( true )
                    ->validate($scene)
                    ->allowField($allowField )
                    ->save($data);

                if (false === $result) {
                    return $this->error($this->model->getError() );
                } else {
                    // echo $this->model->getLastSql();
                    return $this->success('更新成功');
                }
            } else {
                return $this->error( '没有提交数据' );
            }  
        }else{

            $model = model('User');
            $user_id = session('user_auth.user_id');
            $user =  $model->find($user_id);

            $this->assign('info' , $user );
            $this->assign('roles' , $user->role_arr );

            $this->setMeta('个人信息|个人资料');
            return view('info');
        }
        


    }




    public function password(){
        if(IS_AJAX){

            $user_id = session('user_auth.user_id') ;

            if( session('user_auth.user_id') != input('user_id/s') ){ return $this->error('登录权限错误，无法修改密码' );}

            $password0 = input('password0/s');
            $password1 = input('password1/s');
            $password2 = input('password2/s');

            if($password1 != $password2){  return $this->error('两次密码不一样' );}


            $user = $this->model->get($user_id);
            if($user['password'] != md5($password0)){ return $this->error('原始密码错误' );} 



            //修改密码
            $allowField = [ 'password' ];
            $scene = 'user.password';
            $data = [
                'user_id'=> $user_id,
                'password'=>$password1,
            ];
            if ($data) {

                $result = $this->model
                    ->isUpdate( true )
                    ->validate($scene)
                    ->allowField($allowField )
                    ->save($data);

                if (false === $result) {
                    return $this->error($this->model->getError() );
                } else {
                    // echo $this->model->getLastSql();
                    return $this->success('更新成功');
                }
            } else {
                return $this->error( '没有提交数据' );
            }  
        }else{

            $model = model('User');
            $user_id = session('user_auth.user_id');
            $user =  $model->find($user_id);

            $this->assign('info' , $user );
            $this->assign('roles' , $user->role_arr );

            $this->setMeta('个人信息|修改密码');
            return view('password');
        } 
    }

 


    


}
